package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.company.dao.GroupDao;
import com.fz.us.admin.company.entity.Group;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Dao实现类 - 岗位
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-27 。
 * ============================================================================
 */
@Repository
public class GroupDaoImpl extends BaseDaoImpl<Group, String> implements GroupDao {


    @Override
    public List<String> findGroupIdByUser(User user) {
        String sql="SELECT s.id FROM sys_group as s\n" +
                "LEFT JOIN sys_group_depart as srd ON s.id=srd.id\n" +
                "LEFT JOIN sys_department as sd ON sd.id=srd.depid\n" +
                "LEFT JOIN sys_group_power as srp ON srp.id=s.id\n" +
                "LEFT JOIN sys_power as spo ON spo.id=srp.powerid\n" +
                "LEFT JOIN sys_group_sys_user as srsu ON srsu.groupSet_id=s.id\n" +
                "LEFT JOIN sys_user as su ON su.id=srsu.usersSet_id\n" +
                "LEFT JOIN sys_duty as duty ON duty.user_id=su.id\n"+
                "WHERE duty.user_id=:u";
        List<String> groupList = getSession().createSQLQuery(sql).setParameter("u", user.getId()).list();
        if(groupList == null){
            return Lists.newArrayList();
        }
        return groupList;
    }

    @Override
    public Group findGroupByName(String name) {
        Assert.notNull(name,"name is required");
        String hql = " from Group where name = :name and state=:state";
        List<Group> groupList = this.sessionFactory.getCurrentSession().createQuery(hql).setParameter("name", name).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
        if(groupList.size()>0){
            return groupList.get(0);
        }
        return null;
    }
}